//
//  UIViewController+DismissKeyBoard.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/20.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "UIViewController+DismissKeyBoard.h"

@implementation UIViewController (DismissKeyBoard)

-(void)dismissKeyBoard
{
    
    NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    
    LK(weakSelf)
    
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [noti addObserverForName:UIKeyboardWillShowNotification
                      object:nil
                       queue:mainQuene
                  usingBlock:^(NSNotification *note){
                      [weakSelf.view addGestureRecognizer:singleTap];
                  }];
    [noti addObserverForName:UIKeyboardWillHideNotification
                      object:nil
                       queue:mainQuene
                  usingBlock:^(NSNotification *note){
                      [weakSelf.view removeGestureRecognizer:singleTap];
                  }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer
{
    [self.view endEditing:YES];
}

@end
